public class MyLinkList {
    static class LinkNode{
        public int val;
        public LinkNode prev;//前驱
        public LinkNode next;//后继

        public LinkNode(int val) {
            this.val = val;
        }
    }

    public LinkNode head; //头节点

    public LinkNode last; //尾节点
    //链表的打印
    public void disPlay(){
        LinkNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //求链表的大小
    public int  size(){
        int count = 0;
        LinkNode cur = head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键key值在链表当中
    public boolean contains(int key){
        LinkNode cur = head;
        while (cur!=null){
            if(cur.val==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法 O(1)
    public void addFirst(int data){
        LinkNode node = new LinkNode(data);
        if (head == null){
            head = node;
            last = node;
        }else{
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    //尾插法O(1)
    public void addLast(int data){
        LinkNode node = new LinkNode(data);
        if (head == null){
            head = node;
            last = node;
        }else{
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    //任意位置插入
    public void addIndex( int index,int data){
        LinkNode node = new LinkNode(data);
        if (index<0||index>size()){
            return;
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        //找到要插入位置的位置,或者将其写入到一个函数当中
        /*LinkNode cur = head;
        int count = 0;
        while (count<index){
            cur=cur.next;
            count++;
        }*/
        LinkNode cur = findIndex(index);
        //开始插入，需要四步,而且顺序不能交换
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    //找到要插入位置的位置
    private LinkNode findIndex(int index){
        LinkNode cur = head;
        while (index!=0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //删除链表当中
    public void remove(int key){
        LinkNode cur = head;
        while (cur!=null){
            //开始删除
            if (cur.val == key){
                //1.删除的是头节点
                if (cur == head){
                    head = head.next;  //让head往后走
                    //head.prev = null; //这一句是有问题的 ，需要进行判断

                    //只有一个节点的情况下
                    if (head!=null){
                        head.prev = null;
                    }

                }else {
                    // 中间 ， 尾巴
                    cur.prev.next = cur.next; //这一句是两者合并了
                    //先考虑不删除头尾的情况
                   if (cur.next!=null){
                       //不是尾巴节点
                       //cur.prev.next = cur.next;
                       cur.next.prev = cur.prev;
                   }else {
                       //是尾巴节点
                       //cur.prev.next = cur.next;
                       last = last.prev;
                   }
                   // cur.next.prev = cur.prev;
                }
                return;
            }
            cur = cur.next;
        }
    }


    //删除所有值为key的节点
    //只遍历列表一边
    // 直接去掉上面的return就行了

    public void removeAllKey(int key){
        LinkNode cur = head;
        while (cur!=null){
            //开始删除
            if (cur.val == key){
                //1.删除的是头节点
                if (cur == head){
                    head = head.next;  //让head往后走
                    //head.prev = null; //这一句是有问题的 ，需要进行判断

                    //只有一个节点的情况下
                    if (head!=null){
                        head.prev = null;
                    }

                }else {
                    // 中间 ， 尾巴
                    cur.prev.next = cur.next; //这一句是两者合并了
                    //先考虑不删除头尾的情况
                    if (cur.next!=null){
                        //不是尾巴节点
                        //cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else {
                        //是尾巴节点
                        //cur.prev.next = cur.next;
                        last = last.prev;
                    }
                    // cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }

    //清空所有链表
    public void clear(){
       //head = null;
       //last = null;
        //遍历双向链表的每一个节点把前驱和后继都置为空
        LinkNode cur = head;
        while(cur!=null){
            LinkNode curNext = cur.next; //需要先记录一下后继的数据
            //这时候再把cur的前驱和后继置null
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
}
